package presentation;



import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import domain.UserManager;

public class FrontController extends HttpServlet {

        /**
	 * 
	 */
	private static final long serialVersionUID = 1L;

		//String userFrom = "admin";

        //String passwd = "ingenieria";

        public void init() throws ServletException {
        }

        protected void doGet(HttpServletRequest request,
                        HttpServletResponse response) throws ServletException, IOException {
                processRequest(request, response);

        }

        protected void doPost(HttpServletRequest request,
                        HttpServletResponse response) throws ServletException, IOException {
                processRequest(request, response);
        }

        public void processRequest(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            	
        	UserManager um = new UserManager();    
            if (um.logado(request)) {
            	String resource = request.getParameter("res");
                RequestDispatcher d = request.getRequestDispatcher(resource);
                if(d!=null){
                	d.forward(request,response);
                }
                    System.out.println("LOGADO ACCEDIENDO A " + resource);
                }else{
                	String resource = request.getParameter("res");
                	if(resource.equals("formularioregistro.jsp")){
                		RequestDispatcher d = request.getRequestDispatcher(resource);
                        d.forward(request,response);
                    }
                	if(resource.equals("nuevacontraseña.jsp")){
                		RequestDispatcher d = request.getRequestDispatcher(resource);
                        d.forward(request,response);
                    }
                	if(resource.equals("login2.jsp")){
                		RequestDispatcher d = request.getRequestDispatcher(resource);
                        d.forward(request,response);
                    }/*no se si estos 2 if funcionaran sq e pensao q si no estas
                	registrado, cuando le des a registrarse o a nueva contraseña
                	te mandara a la pagina de error*/
                    
                	response.sendRedirect("error.html");
                        /*hay q crear nuestra propia pagina de error,
                        pero en plan de q se ha equivocado de contraseña y no puede
                        acceder...*/
                }

        }

        /*private boolean logado(HttpServletRequest request) {
                boolean logado = false;

                HttpSession session = request.getSession(false);

                String userForm = request.getParameter("user");
                String passwdForm = request.getParameter("passwd");

                if (session == null) {
                        session = request.getSession();
                        if (userForm == null || passwdForm == null
                                        || userForm.length() == 0 || passwdForm.length() == 0) {
                                logado = false;

                        } else {
                                if (valido(userForm, passwdForm)) {
                                        logado = true;
                                        session.setAttribute("session.user", userForm);
                                } else {
                                        logado = false;
                                }

                        }

                } else {
                        if (userForm == null || passwdForm == null) {
                                logado = true;
                        } else {
                                if (valido(userForm, passwdForm)) {
                                        logado = true;
                                        session.setAttribute("session.user", userForm);
                                } else {
                                        logado = false;
                                }

                        }
                }
                return logado;
        }*/

        /*public boolean valido(String userForm, String passwdForm) {
                boolean res = false;
                res = (userForm.equals(this.user) && passwdForm.equals(this.passwd));
                return res;

        }*/

}

